<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item v-if="checkPermi(['platop'])" label="代理" prop="parentName">
        <el-input
          v-model="queryParams.parentName"
          placeholder="输入代理名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="checkPermi(['platop'])" label="商户号" prop="mchNo">
        <el-input
          v-model="queryParams.mchNo"
          placeholder="输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="checkPermi(['platop'])" label="商户名" prop="mchName">
        <el-input
          v-model="queryParams.mchName"
          placeholder="输入商户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <br v-if="checkPermi(['platop'])"/>
      <el-form-item label="收款户名" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="输入收款户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select
          v-model="queryParams.state"
          size="small"
          clearable
          placeholder=""
          class="filter-item"
          style="width: 140px"
        >
          <el-option
            v-for="item in stateOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div>
        <div style="display: flex;">
          <el-card v-if="!checkPermi(['platop'])" shadow="hover" style="cursor: pointer;background-color:#f9f9f9;width: 160px;margin-bottom: 20px"
                   @click.native="withdraw(0)">
            <div style="display: flex;flex-direction: column;align-items: center;">
              <img src="@/assets/va/withdraw.png" width="60">
              <span style="margin-top: 10px">外部转账</span>
            </div>
          </el-card>
          <el-card v-if="!checkPermi(['platop'])" shadow="hover" style="cursor: pointer;background-color:#f9f9f9;width: 160px;margin-bottom: 20px;margin-left: 20px"
                   @click.native="withdraw(1)">
            <div style="display: flex;flex-direction: column;align-items: center;">
              <img src="@/assets/va/internal.png" height="60">
              <span style="margin-top: 10px">内部转账</span>
            </div>
          </el-card>
          <el-card v-if="checkPermi(['platop'])" shadow="hover" style="cursor: pointer;background-color:#f9f9f9;width: 160px;margin-bottom: 20px;margin-left: 20px"
                   @click.native="withdraw(11)">
            <div style="display: flex;flex-direction: column;align-items: center;">
              <img src="@/assets/va/internal.png" height="60">
              <span style="margin-top: 10px">内部转账(运维)</span>
            </div>
          </el-card>
          <el-card v-if="!checkPermi(['platop'])" shadow="hover" style="cursor: pointer;background-color:#f9f9f9;width: 160px;margin-bottom: 20px;margin-left: 20px"
                   @click.native="withdraw(2)">
            <div style="display: flex;flex-direction: column;align-items: center;">
              <img src="@/assets/va/settle.png" height="60">
              <span style="margin-top: 10px">快速结汇</span>
            </div>
          </el-card>
        </div>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="withdrawList" @selection-change="handleSelectionChange">
      <el-table-column v-if="checkPermi(['platop'])"  label="代理" align="center" prop="parentName" />
      <el-table-column v-if="checkPermi(['platop'])" label="商户" align="center"  width="200">
        <template slot-scope="scope">
          <p>
            <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">商户号</span>
            <span>{{ scope.row.mchNo }}</span>
          </p>
          <p>
            <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">名称</span>
            <span>{{ scope.row.mchName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column  label="时间" align="center"  width="210">
        <template slot-scope="scope">
          <p>
            <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">提交</span>
            <span>{{ scope.row.createTime }}</span>
          </p>
          <p v-if="scope.row.finishTime">
            <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">结束</span>
            <span>{{ scope.row.finishTime }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column  label="转账信息" align="center" width="350">
          <template slot-scope="scope">
            <p>
              <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">转账金额</span>
              <span>{{ scope.row.amount }} {{ scope.row.currency }}</span>
            </p>
            <p>
              <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">预计到账</span>
              <span>{{ scope.row.realAmount }} {{ scope.row.currency }}</span>
            </p>
          </template>
      </el-table-column>
      <el-table-column  label="手续费" align="center" width="200">
        <template slot-scope="scope">
          <p>
            <span v-if="checkPermi(['platop'])" style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">手续费</span>
            <span>{{ scope.row.outTax }} {{ scope.row.currency }}</span>
          </p>
          <p v-if="checkPermi(['platop'])">
            <span style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">利润</span>
            <span>{{ scope.row.profit }} {{ scope.row.currency }}</span>
          </p>
        </template>
      </el-table-column>

      <el-table-column  label="转账状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0">处理中</el-tag>
          <el-tag v-if="scope.row.state == 2" type="success">已完成</el-tag>
          <el-tag v-if="scope.row.state == 3" type="danger">失败</el-tag>
          <el-tag v-if="scope.row.state == 4">结汇中</el-tag>
          <el-tag v-if="scope.row.state == 5" type="success">结汇完成</el-tag>
          <el-tag v-if="scope.row.state == 6" type="danger">结汇失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column  label="收款银行/链" align="center" prop="bankName" />
      <el-table-column  label="收款账号" align="center" prop="accountNo" />
      <el-table-column  label="收款户名" align="center" prop="accountName" />
      <el-table-column  label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state === 0 || scope.row.state === 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showCert(scope.row.id)"
          >查看凭证</el-button>
          <el-button
            v-if="scope.row.state === 4"
            size="mini"
            type="mini"
            icon="el-icon-edit"
            @click="showSettleDiaglog(scope.row.id)"
            v-hasPermi="['platop']"
          >结汇处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="结汇人工处理" :visible.sync="settleDialogVisible" center width="30%" height="390">
      <el-row style="width: 100%;text-align: center">
        <div class="text item">
          <el-input
            v-model="auditForm.remark"
            clearable
            size="small"
            placeholder="备注"
          />
        </div>
      </el-row>
      <el-row style="width: 100%;text-align: center;margin-top: 20px">
        <el-button type="primary" @click="audit(5)">结汇完毕</el-button>
        <el-button type="danger" @click="audit(6)" style="margin-left: 60px">结汇驳回</el-button>
      </el-row>
    </el-dialog>
    <!-- 添加或修改VA提款转载对话框 -->
    <el-dialog :title="title" :visible.sync="openWithdraw" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true"  v-if="!checkPermi(['platop'])">
        <div>
          <span v-if="withdrawType === 2">申请结汇的金额</span>
          <span v-else>申请转账金额</span>
        </div>
        <br/>
        <el-select v-model="form.currency" placeholder="请选择" style="width: 30%;margin-top: 5px" @change="changeIcon('withdrawSelect')"  ref="withdrawSelect">
          <el-option
            v-for="(item, key) in balanceMap"
            :key="key"
            :label="key"
            :value="key">
            <div class="option_box">
              <el-image class="option_img" :src="imgMap[key]"></el-image>
              <span>{{item }} {{key}}</span>
            </div>
          </el-option>
        </el-select>
        <el-input style="width: 70%" v-model="form.amount" type="number" placeholder="输入转账金额"></el-input>
        <div style="font-size: 12px;color: #86909c;margin-top: 8px;margin-bottom: 15px">当前账户余额:
          <span v-if="form.currency"> {{balanceMap[form.currency]}}</span>
          <span v-else>0.00</span>
        </div>
        <template v-if="(withdrawType === 0 || withdrawType === 2)">
          <div>收款账户</div><br/>
          <el-select v-model="form.payeeId" placeholder="请选择收款人" style="width: 100%;margin-top: 5px" filterable >
            <el-option
              v-for="item in payeeList"
              v-if="(withdrawType == 2 && item.inChina === 1 || withdrawType == 0) && item.currency === form.currency"
              :key="item.id"
              :label="item.currency + '-' + item.accountNo +  (item.accountName? ('-' +item.accountName):'') + '-' + item.bankName"
              :value="item.id">
            </el-option>
          </el-select>
          <div>转账备注(最多140字)</div><br/>
          <el-input type="textarea"  v-model="form.remark" style="margin-top: 10px"></el-input>
          <div v-if="withdrawType === 0" style="margin-top: 15px">非同名补充材料</div><br/>
          <div v-if="withdrawType === 0" style="width: 100%;text-align: center">
            <el-upload
              class="upload-demo"
              drag
              :headers="headers"
              ref="upload"
              :action="uploadFileUrl"
              :on-success="uploadSuccess"
              :file-list="fileList"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">不超过10MB</div>
            </el-upload>
          </div>
        </template>
        <template v-if="withdrawType === 1">
          <div>收款方平台登录账号</div><br/>
          <el-input type="text"  v-model="form.targetAccount" style="margin-top: 10px"></el-input>
        </template>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true"  v-else>
        <div>
          <span>申请转账金额</span>
        </div>
        <br/>
        <el-input v-model="form.currency" type="text" style="width: 30%;margin-top: 5px" placeholder="输入币种"> </el-input>
        <el-input style="width: 70%;margin-top: 5px;margin-bottom: 15px" v-model="form.amount" type="number" placeholder="输入转账金额"></el-input>
        <div>转出方平台登录账号</div><br/>
        <el-input type="text"  v-model="form.srcAccount" style="margin-top: 10px"></el-input>

        <div>收款方平台登录账号</div><br/>
        <el-input type="text"  v-model="form.targetAccount" style="margin-top: 10px"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWithdraw" V-loading="submitLoading">提交订单</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWithdraw} from  "@/api/fusion/va/withdraw";
import {queryWithdrawInfo, withdraw, auditSettle, downloadCert} from '@/api/fusion/va/module/bison'
import usd from '@/assets/va/usd.png'
import eur from '@/assets/va/eur.png'
import cnh from '@/assets/va/cnh.png'
import usdc from '@/assets/va/usdc.jpg'
import usdt from '@/assets/va/usdt.png'
import {getToken} from "@/utils/auth";
export default {
  name: "Withdraw",
  data() {
    return {
      uploadFileUrl: process.env.VUE_APP_BASE_API  + "/bison/uploadWithdrawFile",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileId:[],
      fileList:[],
      stateOptions: [
        {key: 0, display_name: '处理中'},
        {key: 2, display_name: '转账完成'},
        {key: 3, display_name: '转账失败'},
        {key: 4, display_name: '结汇中'},
        {key: 5, display_name: '结汇完成'},
        {key: 6, display_name: '结汇失败'}
      ],
      settleDialogVisible: false,
      submitLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // VA提款转载表格数据
      withdrawList: [],
      // 弹出层标题
      title: "",
      imgMap: {},
      // 是否显示弹出层
      openWithdraw: false,
      withdrawType: undefined,
      // 查询参数
      queryParams: {
        isAsc: 'desc',
        orderByColumn: 'id',
        pageNum: 1,
        pageSize: 10,
        provider: 'bison',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      balanceMap: {},
      payeeList:[],
      auditForm: {
        id: '',
        remark: '',
        state: ''
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showCert(id) {
      downloadCert(id).then(res=> {
        window.open(res.data)
      })
    },
    uploadSuccess(response) {
      if (response.code === 200) {
        this.fileId.push(response.fileId)
      }
    },
    showSettleDiaglog(id) {
      this.settleDialogVisible = true
      this.auditForm = {}
      this.auditForm.id = id
    },
    audit(state) {
      this.auditForm.state = state
      if (state == 6 && (!this.auditForm.remark || this.auditForm.remark == '')) {
        this.$modal.msgError('订单驳回操作,备注不能为空');
        return
      }
      this.$modal.confirm('请确认是否继续您的操作？').then(() => {
        auditSettle(this.auditForm).then(() => {
          this.$modal.msgSuccess('操作成功')
          this.settleDialogVisible = false
          this.getList()
        })
      })
    },
    changeIcon(refName) {
      let currency = this.form.currency
      this.$refs[refName]
        .$el.children[0]
        .children[0]
        .setAttribute(
          "style",
          `
					background: url(${this.imgMap[currency]}) no-repeat;
					background-position: 10px center;
					background-size: 20px 20px;
					text-indent: 30px;
				`
        );
    },
    updateImgMap() {
      for (var key in this.balanceMap ) {
        if (key == 'EUR') {
          this.imgMap[key] = eur
        } else if (key == 'USD') {
          this.imgMap[key]= usd
        } else if (key == 'CNH') {
          this.imgMap[key] = cnh
        } else if (key == 'USDC') {
          this.imgMap[key]= usdc
        } else if (key == 'USDT') {
          this.imgMap[key] = usdt
        }
      }
    },
    withdraw(withdrawType) {
      this.reset();
      this.balanceMap = {}
      this.payeeList = []
      this.withdrawType = withdrawType
      //0=外部,1=内部,2=结汇
      this.title = "";
      this.fileId = []
      this.fileList = []
      if(!this.checkPermi(['platop'])) {
        queryWithdrawInfo().then(res=>{
          this.payeeList = res.payeeList
          this.balanceMap = res.balance
          this.openWithdraw = true;
          if(this.withdrawType === 0) {
            this.title = "外部转账";
          } else if(this.withdrawType === 1) {
            this.title = "内部转账";
          } else if(this.withdrawType === 2) {
            this.title = "快速结汇";
          }
          this.updateImgMap()
        })
      } else {
        this.title = "内部转账(管理员专用)";
        this.openWithdraw = true;
      }

    },
    submitWithdraw() {
      this.form.withdrawType = this.withdrawType
      this.submitLoading = true
      this.form.files = this.fileId
      withdraw(this.form).then(res=>{
        this.openWithdraw = false;
        this.submitLoading = false
        this.$modal.msgSuccess("提交成功")
        this.getList()
      }).catch(()=>{
        this.submitLoading = false
      })
    },

    /** 查询VA提款转载列表 */
    getList() {
      this.loading = true;
      listWithdraw(this.queryParams).then(response => {
        this.withdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openWithdraw = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.provider = 'bison'
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.state = ''
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除VA提款转载编号为"' + ids + '"的数据项？').then(function() {
    //     return delWithdraw(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    // /** 导出按钮操作 */
    // handleExport() {
    //   this.download('va/withdraw/export', {
    //     ...this.queryParams
    //   }, `withdraw_${new Date().getTime()}.xlsx`)
    // }
  }
};
</script>
<style>
.option_img {
  width: 25px;
  height: 25px;
  margin-right: 7px;
}
</style>
